package com.cowin.sys.controller;

import com.cowin.base.result.Result;
import com.cowin.base.result.ResultGenerator;
import com.cowin.inf.model.KyBdcVehicle;
import com.cowin.inf.service.KyBdcRouteLogService;
import com.cowin.inf.service.KyBdcVehicleService;
import com.cowin.sys.model.TmVhcVehicle;
import com.cowin.sys.model.vo.*;
import com.cowin.sys.service.TmBasUlocService;
import com.cowin.sys.service.TmVhcVehicleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by  on 2019/09/24.
 *
 * @author CodeGenerator
 */
@Slf4j
@RestController
@Api(value = "TmVhcVehicleController", description = "车辆生产信息控制器", tags = "TmVhcVehicleController", consumes = "application/json")
@RequestMapping("/tm/vhc/vehicle")
public class TmVhcVehicleController {
    @Resource
    private TmVhcVehicleService tmVhcVehicleService;
    @Resource
    private KyBdcVehicleService kyBdcVehicleService;
    @Resource
    private KyBdcRouteLogService kyBdcRouteLogService;
    @Resource
    private TmBasUlocService tmBasUlocService;



    @ApiOperation(value = "Hold车辆信息接口", notes = "Hold车辆信息接口")
    @PostMapping("/search")
    public Result searchHoldVehicle(@RequestBody HoldVehicle holdVehicle) {
        PageHelper.startPage(holdVehicle.getPage(), holdVehicle.getLimit());
        List<TmVhcVehicle> list = tmVhcVehicleService.searchHoldVehicle(holdVehicle);
        PageInfo<TmVhcVehicle> pageInfo = new PageInfo<>(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @ApiOperation(value = "焊装在制车辆", notes = "焊装在制车辆")
    @PostMapping("/hz")
    public Result hzOnLineMake(@RequestBody HoldVehicle holdVehicle) {
        Map<String, String> hz20Map = tmBasUlocService.getLineUlocNoBycodeValue("HZ11_20");
        Map<String,Object> map = new HashMap<>();
        if(holdVehicle.getPage()!=-1&&holdVehicle.getLimit()!=-1){
            PageHelper.startPage(holdVehicle.getPage(), holdVehicle.getLimit());
        }
        //查询焊装10-20
        List<OnLineMakeVehicle> hzList = tmVhcVehicleService.onLineMakeByPoint("HZ11_10",holdVehicle.getVin());
        if(holdVehicle.getPage()!=-1&&holdVehicle.getLimit()!=-1) {
            PageInfo<OnLineMakeVehicle> hzListInfo = new PageInfo<>(hzList);
            map.put("hz1020List", hzListInfo);
        }else {
            map.put("hz1020List", hzList);
        }

        if(holdVehicle.getPage()!=-1&&holdVehicle.getLimit()!=-1){
            PageHelper.startPage(holdVehicle.getPage(), holdVehicle.getLimit());
        }
        //查询焊装20-bdc
        List<OnLineMakeVehicle> w2List = kyBdcVehicleService.findByPoint("W2",holdVehicle.getVin());
        String lineNo = hz20Map.get("LINE_NO");
        String ulocNo = hz20Map.get("ULOC_NO");
        w2List.forEach(e->{
            e.setLineNo(lineNo);
            e.setUlocNo(ulocNo);
        });
        if(holdVehicle.getPage()!=-1&&holdVehicle.getLimit()!=-1){
            PageInfo<OnLineMakeVehicle> w2ListInfo = new PageInfo<>(w2List);
            map.put("hz20BDCList", w2ListInfo);
        }else {
            map.put("hz20BDCList", w2List);
        }

        return ResultGenerator.genSuccessResult(map);
    }
    @ApiOperation(value = "涂装在制车辆", notes = "涂装在制车辆")
    @PostMapping("/tz")
    public Result tzOnLineMake(@RequestBody HoldVehicle holdVehicle) {
        if(holdVehicle.getPage()!=-1&&holdVehicle.getLimit()!=-1){
            PageHelper.startPage(holdVehicle.getPage(), holdVehicle.getLimit());
        }
        //查询涂装装10-20
        List<OnLineMakeVehicle> tz1020List = tmVhcVehicleService.onLineMakeByPoint("TZ11_10",holdVehicle.getVin());
        //查询涂装20-BDC
        List<OnLineMakeVehicle> tzList = tmVhcVehicleService.onLineMakeByPoint("TZ11_20",holdVehicle.getVin());
        List<String> vins = new ArrayList<>();
        tzList.forEach(e-> vins.add(e.getVin()));
        List<String> list = kyBdcRouteLogService.filterPoint(vins, "P1");
        List<OnLineMakeVehicle> tz20BDCList = new ArrayList<>();
       for(OnLineMakeVehicle onLineMakeVehicle:tzList){
           if(!list.contains(onLineMakeVehicle.getVin())){
               tz20BDCList.add(onLineMakeVehicle);
           }
       }
        Map<String,Object> map = new HashMap<>();
        if(holdVehicle.getPage()!=-1&&holdVehicle.getLimit()!=-1){
            PageInfo<OnLineMakeVehicle> tz1020ListInfo = new PageInfo<>(tz1020List);
            PageInfo<OnLineMakeVehicle> tz20BDCListInfo = new PageInfo<>(tz20BDCList);
            map.put("tz1020List", tz1020ListInfo);
            map.put("tz20BDCList", tz20BDCListInfo);
        }else {
            map.put("tz1020List", tz1020List);
            map.put("tz20BDCList", tz20BDCList);
        }
        return ResultGenerator.genSuccessResult(map);
    }

    @ApiOperation(value = "总装在制车辆", notes = "总装在制车辆")
    @PostMapping("/zz")
    public Result zzOnLineMake(@RequestBody HoldVehicle holdVehicle) {
        Map<String,Object> map = new HashMap<>();
        if(holdVehicle.getPage()!=-1&&holdVehicle.getLimit()!=-1){
            PageHelper.startPage(holdVehicle.getPage(), holdVehicle.getLimit());
        }
        //查询总装装10-20
        List<OnLineMakeVehicle> zz10List = tmVhcVehicleService.onLineMakeByPoint("ZZ11_10",holdVehicle.getVin());
        if(holdVehicle.getPage()!=-1&&holdVehicle.getLimit()!=-1){
            PageInfo<OnLineMakeVehicle> zz1020ListInfo = new PageInfo<>(zz10List);
            map.put("zz1020", zz1020ListInfo);
        }else {
            map.put("zz1020", zz10List);
        }
        //查询总装装10-20
        if(holdVehicle.getPage()!=-1&&holdVehicle.getLimit()!=-1){
            PageHelper.startPage(holdVehicle.getPage(), holdVehicle.getLimit());
        }
        List<OnLineMakeVehicle> zz20List = tmVhcVehicleService.onLineMakeByPoint("ZZ11_20",holdVehicle.getVin());

        if(holdVehicle.getPage()!=-1&&holdVehicle.getLimit()!=-1){
            PageInfo<OnLineMakeVehicle> zz2030ListInfo = new PageInfo<>(zz20List);
            map.put("zz2030", zz2030ListInfo);
        }else {
            map.put("zz2030", zz20List);
        }
        return ResultGenerator.genSuccessResult(map);
    }

    @ApiOperation(value = "车辆细节查询", notes = "车辆细节查询")
    @PostMapping("/details")
    public Result getVehicleDetails(@RequestBody VehicleDetails vehicleDetails) {
        if(vehicleDetails.getPage()!=-1&&vehicleDetails.getLimit()!=-1){
            PageHelper.startPage(vehicleDetails.getPage(), vehicleDetails.getLimit());
            List<VehicleDetails> list = tmVhcVehicleService.getVehicleDetails(vehicleDetails);
            PageInfo<VehicleDetails> detailsPageInfo = new PageInfo<>(list);
            return ResultGenerator.genSuccessResult(detailsPageInfo);
        }else {
            List<VehicleDetails> list = tmVhcVehicleService.getVehicleDetails(vehicleDetails);
            return ResultGenerator.genSuccessResult(list);
        }
    }

    @ApiOperation(value = "获取车型配置", notes = "获取车型配置")
    @GetMapping("/type/config")
    public Result getVehicleType() {
        List<SysCodeVo> list  = tmVhcVehicleService.getVehicleType();
        return ResultGenerator.genSuccessResult(list);
    }
    @ApiOperation(value = "车辆颜色配置", notes = "车辆颜色配置")
    @GetMapping("/color/config")
    public Result getVehicleColorConfig() {
        List<SysCodeVo> list = tmVhcVehicleService.getVehicleColorConfig();
        return ResultGenerator.genSuccessResult(list);
    }
}
